/**
 * 
 */
package com.bjtech.web.action;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.bjtech.dao.UtilDao;
import com.bjtech.domain.User;
import com.bjtech.domain.UserInfo;
import com.bjtech.web.util.Constants;
import com.bjtech.web.util.DateUtil;

/**
 * @author 
 */
public class LoginAction {
	private String username;

	private String password;

	private String validity;

	private int user_type;

	private UtilDao utilDao;// = (UtilDao)InitServletContextListener.getBean("utilDao");

	public String message;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getValidity() {
		return validity;
	}

	public void setValidity(String validity) {
		this.validity = validity;
	}

	public int getUser_type() {
		return user_type;
	}

	public void setUser_type(int user_type) {
		this.user_type = user_type;
	}

	public String login() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = ServletActionContext.getRequest().getSession();
		String validity = request.getParameter("validity");
		String val = session.getAttribute("validity") != null ? session.getAttribute("validity").toString() : "";

		String forward = "error";

		if (!session.isNew()) {
			session.invalidate();
			session = request.getSession(true);
			session.setMaxInactiveInterval(30 * 60);
		}

		try {

			if (StringUtils.isEmpty(username)) {
				message = "用户名为空！";
			} else if (StringUtils.isEmpty(validity) || StringUtils.isEmpty(val) || !val.equalsIgnoreCase(validity)) {
				message = "验证码为空！";
			} else {
				User user = new User();
				//后台管理员
					UserInfo eu = utilDao.getUserInfoByUserName(username);

					if (eu != null && eu.getId() > 0 && (eu.getPass_word()!= null && eu.getPass_word().equals(password))) {
						user.setUserId(eu.getId());
						user.setUsername(eu.getUser_name());
						user.setRealname(username);
						user.setLoginTimer(Calendar.getInstance().getTime());
						user.setUserRole(eu.getUser_role());
						user.setProjectId(eu.getProj_id());
//						if(user.getUserRole()<5){
							forward = "management";
//						}else{
//							forward = "cusotmer";
//						}
						session.setAttribute(Constants.MANAGEMENT, user);
					} else {
						message = "用户名或密码错误！";
					}

				if (user.getUserId() != null) {
					user.setUsername(username);
					user.setSessionId(session.getId());
					user.setRemoteAddr(request.getRemoteAddr());
					String welcome = DateUtil.getDateTime("yyyy-MM-dd HH:mm", new Date());
					user.setWelcome(welcome);
					user.setLoginTimer(Calendar.getInstance().getTime());
				}
				if (user.getUserId() != null) {
					return forward;
				} else {
					message = message.length() > 0 ? message : "用户或密码错误！";
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			message = e.toString();
		}
		request.setAttribute("error", message);
		return "error";
	}

	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();

		session.removeAttribute(Constants.MANAGEMENT);

		session.invalidate();

		return "success";
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

}
